package efi.easycooking.eng;

import utils.Utils;
import efi.easycooking.ImageAdapter;
import efi.easycooking.R;

import efi.easycooking.bo.BaseResponse;
import efi.easycooking.entities.ImageTextSwitcher;
import efi.easycooking.rest.ApiService;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class EngRecetasActivity extends Activity {

	private static String TAG = "EngRecetasActivity";
		
	private int mPosition;
	    private TextSwitcher txSwitcherRecetas;
	    private ImageSwitcher imSwitcherRecetas;
	    private TextSwitcher txSwitcherTituloRecetas;
	    
	    private RecetasEng recetas;
	    
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_eng_recetas);

		Log.d(TAG, "[onCreate] Comenzando Recetas Oncreate");
		
		mPosition = getIntent().getIntExtra("page",1);
		Log.d(TAG, "[onCreate] mPosition= "+mPosition);
		
		txSwitcherTituloRecetas = (TextSwitcher) findViewById(R.id.textSwitcherTitulo);
		txSwitcherTituloRecetas.setFactory(new ViewFactory() {
	            @Override
	            public View makeView() {
	                TextView textView = new TextView(EngRecetasActivity.this);
	                textView.setTextColor(getResources().getColor(R.color.White));
	                textView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
	                textView.setTextSize(15);
	                textView.setGravity(Gravity.CENTER);
	                
	                //textView.setGravity(Gravity.CENTER);
	                return textView;
	            }
	        });
	 
		txSwitcherTituloRecetas.setInAnimation(this, android.R.anim.fade_in);
		txSwitcherTituloRecetas.setOutAnimation(this, android.R.anim.fade_out);
		
		txSwitcherRecetas = (TextSwitcher) findViewById(R.id.textSwitcher);
		txSwitcherRecetas.setFactory(new ViewFactory() {
	            @Override
	            public View makeView() {
	                TextView textView = new TextView(EngRecetasActivity.this);
	                textView.setTextColor(getResources().getColor(R.color.White));
	                textView.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
	                textView.setTextSize(12);
	                
	                //textView.setGravity(Gravity.CENTER);
	                return textView;
	            }
	        });
	 
		txSwitcherRecetas.setInAnimation(this, android.R.anim.fade_in);
		txSwitcherRecetas.setOutAnimation(this, android.R.anim.fade_out);
	 
		imSwitcherRecetas = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		imSwitcherRecetas.setFactory(new ViewFactory() {
	            @Override
	            public View makeView() {
	                ImageView imageView = new ImageView(EngRecetasActivity.this);
	                return imageView;
	            }
	        });
		imSwitcherRecetas.setInAnimation(this, android.R.anim.slide_in_left);
	    imSwitcherRecetas.setOutAnimation(this, android.R.anim.slide_out_right);
	 	    
	    
	    /////////////////////////////////////////7
	    recetas=new RecetasEng();
	    
	    for(ImageTextSwitcher imgSwt : recetas.lstImgRecetas)
	    {
	    	if(mPosition==imgSwt.getIndice())
	    	{	    		
		    	imSwitcherRecetas.setBackgroundResource(imgSwt.getInImageRecetas());	    				    			    	
			    txSwitcherRecetas.setText(imgSwt.getStrRecetas());
			    txSwitcherTituloRecetas.setText(imgSwt.getStrTitulo());
			    break;
	    	}
	    }
	    
	    
	    
	    ImageButton ibAtras=(ImageButton)findViewById(R.id.atrasReceta);		
		ibAtras.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	
		    	mPosition-=1;

		    	//Log.d(TAG, "[onClick] Atras mPosition= "+mPosition);	//DEBUG
		    	
		    	if(mPosition<1)
		    		mPosition=recetas.lstImgRecetas.size();
		    		
		    	//Log.d(TAG, "[onClick] Atras mPosition <1 = "+mPosition); //DEBUG
		    	
		    	for(ImageTextSwitcher imgSwt : recetas.lstImgRecetas)
			    {
			    	if(mPosition==imgSwt.getIndice())
			    	{	    		
				    	imSwitcherRecetas.setBackgroundResource(imgSwt.getInImageRecetas());	    				    			    	
					    txSwitcherRecetas.setText(imgSwt.getStrRecetas());
					    txSwitcherTituloRecetas.setText(imgSwt.getStrTitulo());
					    break;
			    	}
			    }		    	
		    }
		});
	
		
	    ImageButton ibDelante=(ImageButton)findViewById(R.id.delantReceta);		
		ibDelante.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	
		    	mPosition+=1;
		    	
		    	//Log.d(TAG, "[onClick] Delante mPosition= "+mPosition);	//DEBUG
		    	
		    	if(mPosition>recetas.lstImgRecetas.size())
		    		mPosition=1;
		    		
		    	//Log.d(TAG, "[onClick] Delante mPosition > = "+mPosition);	//DEBUG
		    	
		    	for(ImageTextSwitcher imgSwt : recetas.lstImgRecetas)
			    {
			    	if(mPosition==imgSwt.getIndice())
			    	{	    		
				    	imSwitcherRecetas.setBackgroundResource(imgSwt.getInImageRecetas());	    				    			    	
					    txSwitcherRecetas.setText(imgSwt.getStrRecetas());
					    txSwitcherTituloRecetas.setText(imgSwt.getStrTitulo());
					    break;
			    	}
			    }		    
		    }
		});
		
		ImageButton ibSalirRecetas=(ImageButton)findViewById(R.id.ibSalirRecetas);		
		ibSalirRecetas.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	
		    	finish();
		    }
		});
	}

	
	@Override
	public void onBackPressed() {
		Log.d(this.getLocalClassName(), "onBackPressed()");
		this.finish();
	}
}